package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class WokejiNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.wokeji.com/.*/[0-9]+/t[0-9]+_[0-9]+.shtml",
		"http://www.wokeji.com/[a-z]*/[0-9]*/[a-z]*/[a-z0-9]*/[0-9]*/t[0-9]*_[0-9]*.shtml"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
			if (url.indexOf("/kjrc/")>0) {
				return false;
			}
		}
		return false;
	}
	
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();

		//if(!isDetailPage(url)){
		//s}

		news.setUrl(url);
		String title=null;
		String content=null;
		String author = null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		/*
		 * 标题
		 */
		title=doc.select("div#artibodyTitle h1").text();
		if (StringUtils.isBlank(title)){
			title=doc.select("div.vid_blue1").text();
		}
		if (StringUtils.isBlank(title)){
			title=doc.select("div.box2").text();
		}
		/*
		 * 内容
		 */
		//content=HtmlCleaner.getContentHtml(url,doc.select("div#daodu,div#zhengwen"));
		content =HtmlCleaner.getContentHtml(url, doc.select("div.TRS_Editor"));
		if(StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
		}

		/*
		 * 时间
		 */
		date=DateUtils.matchDate(doc.select("div#pub_date").text());
		if (date==null){
			date=JSoupUtils.matchDate(doc, "来源：");
		}
		/*
		 * 链接
		 */
		
		//author=StringUtils.substringAfter(doc.select("div.origin").text(), "来源：");
		/*
		 * 来源
		 */
		author=doc.select("html body div#webpage div div div.box3 p font.font5").text();
		if (StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc, "来源：");
		}
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		WokejiNewsAnalyse analyse=new WokejiNewsAnalyse();
		String url="http://www.wokeji.com/shouye/kjrc/201504/t20150430_1115635.shtml";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		//System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
		System.out.println(url.indexOf("/kjrc/"));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
	public boolean isNeedUpdate(){
		return false;
	}

}
